/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;
import Connections.Database;
import Entities.Administrator;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Administrator
 */
public class AdminManage extends Database{
  Connection conn=null;
    public AdminManage()
    {
      
     }
    public boolean AddAdmin(Administrator obj)
    {
          conn=getConn();
        try {
            PreparedStatement prsm = conn.prepareStatement("INSERT INTO Administrator "
                     + "("
                     + "name"
                     + ",username"
                     + ",password"
                     + ",email"
                     + ",usertype,"
                     + "status,"
                     +"registerDate"
                     +",lastvisitDate"
                     + ",modifiedDate) VALUES(?,?,?,?,?,?,?,?,?)");
             prsm.setString(1, obj.getName());
             prsm.setString(2, obj.getUserName());
             prsm.setString(3,obj.getPassWord());
             prsm.setString(4, obj.getEmail());
             prsm.setString(5, obj.getUserType());
             prsm.setInt(6, obj.getStatus());
             prsm.setDate(7,obj.getRegisterDate());
             prsm.setDate(8,obj.getLastvisitDate());
             prsm.setDate(9,obj.getModifiedDate());
             prsm.execute();
             closeConn();
             return true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert error !");
            //System.out.println(ex);
           closeConn();
           return false;
        }
       
       
    }
 public ArrayList<Administrator> GetListAccount()
  {
     conn=getConn();
      ArrayList<Administrator> arrList=new ArrayList<Administrator>();
         PreparedStatement prsm;
        try {
            prsm = conn.prepareStatement("Select * from Administrator");
            ResultSet rs=prsm.executeQuery();
         while(rs.next())
         {
            Administrator objAdmin = new Administrator(rs.getInt("id"), rs.getString("name"),
                    rs.getString("username"), rs.getString("password"), rs.getString("email"), 
                    rs.getString("usertype"),rs.getInt("status"), rs.getDate("registerDate"),
                    rs.getDate("lastvisitDate"),
                    rs.getDate("modifiedDate"));
             arrList.add(objAdmin);
         }
         closeConn();
        } catch (SQLException ex) {
            System.out.println("Error getList()");
            Logger.getLogger(AdminManage.class.getName()).log(Level.SEVERE, null, ex);
        }


    return arrList;
  }
 public void DeleteAccount(int id)
  {
     conn=getConn();


      try {
          PreparedStatement preStm = conn.prepareStatement("SELECT usertype FROM Administrator WHERE id = ?");
          preStm.setInt(1, id);
          ResultSet rs = preStm.executeQuery();
          rs.next();
          if(rs.getString("usertype").equalsIgnoreCase("Administrator") ){
                JOptionPane.showMessageDialog(null, "Not delete Administrator");
          }else{
                PreparedStatement prsm=conn.prepareStatement("Delete from"
                    + " Administrator where id=?");
                prsm.setInt(1, id);
                prsm.executeUpdate();
                closeConn();
          }
            
      } catch (Exception e) {
          closeConn();
          JOptionPane.showMessageDialog(null, "Delete Error !");
      }

  }
 public String GetStatus(int id)
   {
      conn=getConn();
      String status="";
       try {
                PreparedStatement prsm=conn.prepareStatement("Select status from Administrator where id=?");
                prsm.setInt(1, id);
                ResultSet rs=prsm.executeQuery();
                while(rs.next())
                {
                   status=rs.getString("status");
                }
                closeConn();
       } catch (Exception e) {
           e.printStackTrace();
           closeConn();
       }
      return status;
   }
  public String GetUserType(int id)
   {
      conn=getConn();
      String userType="";
       try {
                PreparedStatement prsm=conn.prepareStatement("Select usertype from Administrator where id=?");
                prsm.setInt(1, id);
                ResultSet rs=prsm.executeQuery();
                while(rs.next())
                {
                   userType=rs.getString("usertype");
                }
                closeConn();
       } catch (Exception e) {
           e.printStackTrace();
           closeConn();
       }
      return userType;
   }
  public Administrator GetListByID(int id)
   {
     conn=getConn();
     Administrator objAdmin=null;
       try {
             PreparedStatement prsm=conn.prepareStatement("select * from Administrator where id=?");
             prsm.setInt(1, id);
             ResultSet rs=prsm.executeQuery();
             while(rs.next())
             {
                 objAdmin=new Administrator(rs.getInt(1),rs.getString(2)
                         ,rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7)
                         ,rs.getInt(8),rs.getDate(9),rs.getDate(10),rs.getDate(11));
             }
             closeConn();

       } catch (Exception e) {
           e.printStackTrace();
       }
     return objAdmin;
  }
   public boolean UpdateAccount(Administrator objBooth,int id)
    {
       conn=getConn();
        try {
            PreparedStatement prsm = conn.prepareStatement("UPDATE Administrator set name=?"
                    + ",username=?"
                    + ",password=?"
                    + ",email=?"
                    + ",usertype=?"
                    + ",status=?"
                    + ",modifiedDate=? where id=?");
             prsm.setString(1, objBooth.getName());
             prsm.setString(2, objBooth.getUserName());
             prsm.setString(3,objBooth.getPassWord());
             prsm.setString(4, objBooth.getEmail());
             prsm.setString(5, objBooth.getUserType());
             prsm.setInt(6, objBooth.getStatus());
             prsm.setDate(7,objBooth.getModifiedDate());
             prsm.setInt(8, id);
             prsm.executeUpdate();
             closeConn();
             return true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "update error !");
            closeConn();
            return false;
        }

     }
}
